using System;
using System.Collections;
using System.Windows.Forms;

namespace Meta.Editor
{
	/// <summary>
	/// Summary description for WarningMenu.
	/// </summary>
	/// 

	public class WarningMenu : ToolStripMenuItem
	{
		IList instances;

		public WarningMenu(string message, IList instances) : base() {
			this.instances = instances;
			Text = message;
			if (instances is ArrayList) {
				((ArrayList)instances).Sort();
			}
			try {
				string lastDetail = null;
				foreach (Warning w in instances) {
					if (w.detail!=lastDetail) {
						ToolStripMenuItem m = new ToolStripMenuItem(w.detail);
						//m.BarBreak = true;
						DropDownItems.Add(m);
						lastDetail = w.detail;
					}
					DropDownItems.Add(new ExpressionMenuItem(w));
				}
			} catch {
			}
		}
		public static bool InspectOnSelect = false;
	}

	public class ExpressionMenuItem : ToolStripMenuItem {
		Warning warning;
		public ExpressionMenuItem(Warning w) {
			warning = w;
			if (w.expression!=null)
				Text = Meta.Writer.SafeWriteToString(w.expression, 30)+" : "+CompactName(w.expression.InferredType);
			else
				Text = "<error - expression is null>";
		}

		static string CompactName(Type t) {
			if (t==null)
				return "unknown";
			else if (t is PseudoType)
				return t.ToString();
			else
				return t.Name;
		}

		protected override void OnClick(EventArgs e) {
			if (WarningMenu.InspectOnSelect)
				Inspector.Inspect(warning.expression);
			else
				Meta.Editor.Editor.EditSource(warning.site);
		}
	}

}
